﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.StateMachine.Engine
{
    public class Util
    {

    }

    public static class Addon
    {

        public static List<TRecordDebugInfo> GetTRecordDebugInfos(this TRecord record, int depth = 0)
        {
            try
            {
                if (depth > 5) return default;
                List<TRecordDebugInfo> debugInfos = new List<TRecordDebugInfo>();
                debugInfos.Add(new TRecordDebugInfo
                {
                    RecordIdentifier = record.TRecordID,
                    Record = record,
                    ChangeSet = record.ChangeSet,
                    ChangeSetItems = record.ChangeSet.ChangeSetItems,
                });

                foreach (var attr in record.GetProperties())
                {
                    if (record[attr] is TRecord childRecord && childRecord != null)
                    {
                        var result = GetTRecordDebugInfos(childRecord, depth + 1);
                        if (result != null && result.Count > 0)
                            debugInfos.AddRange(result);
                    }
                    else
                    {
                        if (record[attr] is List<TRecord> list && list != null && list.Count > 0)
                        {
                            foreach (var child in list)
                            {
                                var result = GetTRecordDebugInfos(child, depth + 1);
                                if (result != null && result.Count > 0)
                                    debugInfos.AddRange(result);
                            }
                        }
                    }
                }
                return debugInfos;

            }
            catch (Exception e)
            {
                return null;
            }
        }
    }

    public class TRecordDebugInfo
    {
        public TRecord Record { get; set; }
        public TChangeSet ChangeSet { get; set; }
        public List<TChangeSetItem> ChangeSetItems { get; set; }
        public object OtherInfo { get; set; }
        public string RecordIdentifier { get; set; }
    }
}
